package com.atguigu.edu.controller;


import com.atguigu.base.ExceptionHnadler.CustomException;
import com.atguigu.edu.entity.vo.CourseInfoVo;
import com.atguigu.edu.entity.vo.CoursePublishVo;
import com.atguigu.edu.entity.vo.CourseQuery;
import com.atguigu.edu.service.CourseService;
import com.atguigu.edu.entity.Course;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ld
 * @since 2022-08-16
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
@Api(description = "课程基本信息管理")
public class CourseController {
    @Resource
    private CourseService courseService;
    //条件组合分页进行课程信息的展示
    @PostMapping("/list/{page}/{size}")
    public R listCourse(@PathVariable Integer page,@PathVariable Integer size,
                        @RequestBody(required = false) CourseQuery courseQuery){
        Page pageInfo= courseService.pageQuery(page,size,courseQuery);
        long total = pageInfo.getTotal();
        List<Course> records = pageInfo.getRecords();
        return R.ok().data("total",total).data("list",records);
    }
    //课程的删除
    @DeleteMapping("/delete/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        boolean b = courseService.removeCourse(courseId);
        if(b){
            return R.ok();
        }else{
            throw new CustomException(20001,"删除课程失败");
        }
    }
    //课程基本信息的添加
    @PostMapping("/add")
    public R addCourse(@RequestBody CourseInfoVo courseInfoVo){
        String id=courseService.addCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfoVo);
    }
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }
    //最终课程发布信息查询
    @GetMapping("/getCoursePublishInfo/{courseId}")
    public R getCoursePublishInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo=courseService.getCoursePublishInfo(courseId);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }
    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{courseId}")
    public R publicCourse(@PathVariable String courseId){
        Course course=new Course();
        course.setId(courseId);
        course.setStatus("Normal");
        boolean b = courseService.updateById(course);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }
    }

}

